//
//  ATVNumericPropertyViewController.m
//  FlashController
//
//  Created by Martin Schiøth Dyrby on 12/1/11.
//  Copyright 2011 Molamil. All rights reserved.
//

#import "ATVNumericPropertyViewController.h"
#import "ATVInstanceView.h"
#import "ATVInstanceNumericProperty.h"

@implementation ATVNumericPropertyViewController

@synthesize titleLabel;
@synthesize currentValueLabel;
@synthesize minimumValueLabel;
@synthesize maximumValueLabel;
@synthesize valueSlider;

@synthesize max;
@synthesize min;
@synthesize isMethod;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Initialization code here.
        min = 0.0;
        max = 1.0;
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateValueHandler:) name:@"updateValue" object:nil];
    }

    
    return self;
}

- (void) destroy {

}

- (void) updateValueHandler: (NSNotification*) note {
    NSDictionary* dict = (NSDictionary*)note.object;
    NSString* path = [dict valueForKey:@"path"];
    
    if([path isEqualToString:self.path]) {

        NSString* value = [dict valueForKey:@"value"];
        double curValue = [value doubleValue];
        
        if(curValue > max) {
            max = curValue + fabs(curValue);
            [self updateMaxMinLabels];
        }
        if(curValue < min) {
            min = curValue - fabs(curValue); 
            [self updateMaxMinLabels];
        }
        [currentValueLabel setDoubleValue:curValue];
        [valueSlider setDoubleValue:curValue];
    }
}

- (void) drawFromModel:(ATVInstanceNumericProperty*) model {
    [titleLabel setStringValue: model.name];
    
    isMethod = model.isMethod;
    [[self methodButton] setHidden: !isMethod];
    
    if([model.name isEqualToString: @"rotation"] ||
              [model.name isEqualToString: @"rotationX"] ||
              [model.name isEqualToString: @"rotationY"] ||
              [model.name isEqualToString: @"rotationZ"]){
        min = -180.0;
        max = 180.0;        
    } else if([model.name isEqualToString: @"x"] ||
              [model.name isEqualToString: @"y"] ||
              [model.name isEqualToString: @"z"]){
        min = 0.0;
        max = 1000.0;        
    }

    [self updateMaxMinLabels]; 
}
- (void) midiHandler: (NSNumber*) value {
    NSLog(@"midiHandler");
	float minimum = [[minimumValueLabel stringValue] floatValue];
	float maximum = [[maximumValueLabel stringValue] floatValue] - minimum;
	float midiMaxi = 127.0f;	
    
	NSNumber* calculatedValue = [NSNumber numberWithFloat:(([value floatValue]/midiMaxi)*maximum+minimum)];
    [self sendValue: [calculatedValue stringValue]];
}
- (IBAction)sliderChangeMessage:(id)sender {
    NSString* value = [valueSlider stringValue];
    [self sendValue: value];
}

- (void) sendValue: (NSString*) value {
    [currentValueLabel setStringValue:value];
    [valueSlider setStringValue:value];
    NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:value, @"value", self.path, @"path", nil];
    
    if(!isMethod){
        [[NSNotificationCenter defaultCenter] postNotificationName:@"setSimpleValue" object:dict];
    }

}

- (void) updateMaxMinLabels {
    [maximumValueLabel setDoubleValue: max];
    [valueSlider setMaxValue: max];
    [minimumValueLabel setDoubleValue: min];
    [valueSlider setMinValue: min];
}

- (IBAction)methodMessage:(id)sender {
    NSString* value = [valueSlider stringValue];
    [currentValueLabel setStringValue:value];
    NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:value, @"value", self.path, @"path", nil];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"executeMethod" object:dict];    
}

- (IBAction)minimumStepperMessage:(id)sender{
    min += 1.0;
    [self updateMaxMinLabels];
}
- (IBAction)maximumStepperMessage:(id)sender{
    max += 1.0;
    [self updateMaxMinLabels];
}
- (IBAction)minimumChangeMessage:(id)sender{
    NSTextField *tf = sender;
    min = [tf doubleValue];
    [self updateMaxMinLabels];
}
- (IBAction)maximumChangeMessage:(id)sender{
    NSTextField *tf = sender;
    max = [tf doubleValue];
    [self updateMaxMinLabels];
}

@end